package com.czu.service.impl;

import com.czu.service.ValidateForForm;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class ValidateForFormImpl implements ValidateForForm {
    @Autowired
    private TbUserMapper mapper;

    @Override
    public boolean validate(Integer type, String content) {
        if(content == null || content.equals(""))
        {
            return false;
        }
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        //type 1(用户名重复检查) 2(电话重复检查) 3(邮箱重复检查)
        if(type == 1)
        {
            criteria.andUsernameEqualTo(content);
        }
        else if(type == 2)
        {
            criteria.andPhoneEqualTo(content);
        }
        else if(type == 3)
        {
            criteria.andEmailEqualTo(content);
        }
        else
        {
            return false;
        }
        List<TbUser> tbUsers = mapper.selectByExample(example);
        if(tbUsers == null || tbUsers.size() == 0)
        {
            return true;
        }
        return false;
    }
}
